﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JZElectrick {

    public partial class DormSelectForm : Form {
        public DormSelectForm() {
            InitializeComponent();
        }
        
        private class DormItem {
            string id;
            string name;
            string rawName;
            public DormItem(string id, string name){
                this.id = id;
                this.name = name;
                this.rawName = name;
            }
            public DormItem(string id, string name, string rawName) : this(id, name) {
                this.rawName = rawName;
            }
            public string ID {
                get { return id; }
            }
            public string Name {
                get { return name; }
            }
            public string RawName {
                get { return rawName; }
            }
        };

        List<DormItem> dormList = null;
        List<DormItem> floorList = null;
        List<DormItem> roomList = null;

        EBService eb = null;

        string selectedBuildName = "";
        string selectedRoomName = "";

        public delegate void DormSelectFinishDelegate(bool back);
        public DormSelectFinishDelegate DormSelectFinishEvent;


        bool finish = false;
        
        public DormSelectForm(string[] list, EBService eb) : this() {
            this.eb = eb;
            dormList = new List<DormItem>();
            for (int i = 0; i < list.Length; i++) {
                string[] compo = list[i].Split(',');
                if (compo.Length > 1) {
                    dormList.Add(new DormItem(compo[0], compo[1]));
                }
            }
        }

        private void DormSelectForm_Load(object sender, EventArgs e) {
            dormListBox.DataSource = dormList;
            dormListBox.DisplayMember = "Name";
            dormListBox.SelectedIndex = -1;
            dormListBox.Enabled = true;
        }

        private void dormListBox_SelectedIndexChanged(object sender, EventArgs e) {
            if (dormListBox.SelectedIndex >= 0 && dormListBox.SelectedIndex < dormList.Count && dormListBox.Enabled) {
                selectedBuildName = dormList[dormListBox.SelectedIndex].Name;
                fullNmaeLabel.Text = selectedBuildName;

                string buildID = dormList[dormListBox.SelectedIndex].ID;
                dormListBox.SetBounds(dormListBox.Bounds.X, dormListBox.Bounds.Y, 150, dormListBox.Bounds.Height);
                floorListBox.Visible = true;
                submitBtn.Enabled = false;

                ((Action)delegate () {
                    string[] list = eb.LoadList(buildID, null);
                    if (list != null) {
                        floorList = new List<DormItem>();
                        for (int i = 0; i < list.Length; i++) {
                            string[] compo = list[i].Split(',');
                            if (compo.Length > 1) {
                                string[] compo2 = compo[1].Split('-');
                                if (compo2.Length > 1) {
                                    floorList.Add(new DormItem(compo[0], compo2[1], compo[1]));
                                }
                            }
                        }

                        this.BeginInvoke((Action)delegate () {
                            
                            floorListBox.DataSource = floorList;
                            floorListBox.DisplayMember = "Name";
                            floorListBox.SelectedIndex = -1;
                            floorListBox.Enabled = true;
                        });
                    }
                }).BeginInvoke(null, null);

                
            }
        }

        private void floorListBox_SelectedIndexChanged(object sender, EventArgs e) {
            if (floorListBox.SelectedIndex >= 0 && floorListBox.SelectedIndex < floorList.Count && floorListBox.Enabled) {
                string buildID = dormList[dormListBox.SelectedIndex].ID;
                string floorID = floorList[floorListBox.SelectedIndex].ID;
                dormListBox.SetBounds(dormListBox.Bounds.X, dormListBox.Bounds.Y, 100, dormListBox.Bounds.Height);
                floorListBox.SetBounds(145, floorListBox.Bounds.Y, 99, floorListBox.Bounds.Height);
                roomListBox.Visible = true;
                submitBtn.Enabled = false;

                ((Action)delegate () {
                    string[] list = eb.LoadList(buildID, floorID);
                    if (list != null) {
                        roomList = new List<DormItem>();
                        for (int i = 0; i < list.Length; i++) {
                            string[] compo = list[i].Split(',');
                            if (compo.Length > 1) {
                                string[] compo2 = compo[1].Split('-');
                                if (compo2.Length > 1) {
                                    roomList.Add(new DormItem(compo[0], compo2[1], compo[1]));
                                }
                            }
                        }

                        this.BeginInvoke((Action)delegate () {

                            roomListBox.DataSource = roomList;
                            roomListBox.DisplayMember = "Name";
                            roomListBox.SelectedIndex = -1;
                            roomListBox.Enabled = true;
                        });
                    }
                }).BeginInvoke(null, null);
            }
        }

        private void roomListBox_SelectedIndexChanged(object sender, EventArgs e) {
            if (roomListBox.SelectedIndex >= 0 && roomListBox.SelectedIndex < roomList.Count && roomListBox.Enabled) {
                selectedRoomName = roomList[roomListBox.SelectedIndex].Name;
                fullNmaeLabel.Text = selectedBuildName + selectedRoomName;
                submitBtn.Enabled = true;
            }
        }

        private void submitBtn_Click(object sender, EventArgs e) {
            string[] dormData = new string[7];
            dormData[0] = selectedBuildName + selectedRoomName;
            dormData[1] = dormList[dormListBox.SelectedIndex].RawName;
            dormData[2] = floorList[floorListBox.SelectedIndex].RawName;
            dormData[3] = roomList[roomListBox.SelectedIndex].RawName;
            dormData[4] = dormList[dormListBox.SelectedIndex].ID;
            dormData[5] = floorList[floorListBox.SelectedIndex].ID;
            dormData[6] = roomList[roomListBox.SelectedIndex].ID;

            foreach (string item in dormData) {
                if (item == null || item == "") {
                    
                    return;
                }
            }

            eb.SaveDorm(new Dorm(dormData[0], dormData[1], dormData[2], dormData[3], dormData[4], dormData[5], dormData[6]));
            DormSelectFinishEvent(false);
            finish = true;
            Close();
        }

        private void DormSelectForm_FormClosing(object sender, FormClosingEventArgs e) {
            if (!finish) {
                if (MessageBox.Show("退出?", "提示", MessageBoxButtons.YesNo) == DialogResult.No) {
                    e.Cancel = true;
                }
            }
            
        }

        private void DormSelectForm_FormClosed(object sender, FormClosedEventArgs e) {
            DormSelectFinishEvent(true);
        }
    }
}
